﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Site.ViewModels;
using SDT.Lib.Common;
using System.Linq.Dynamic;
using SDT.Site.Properties;
using SDT.Lib.Paging;

namespace SDT.Site.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/
        SDTDataContext db = new SDTDataContext();
        public ActionResult Index(int? id)
        {
            try
            {
                ArticleIndexViewModel a = new ArticleIndexViewModel();
                var catid = id.HasValue ? id.Value : 0;
                var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("sitearticle.PageNumber", "page", 1));
                var pageIndex = pageNumber - 1;
                a.Articles = ArticleManager.GetArticles(catid, string.Empty, 0, 1, "ID", "desc", pageIndex, Utils.ParseInt(Settings.Default.PageSize));
                a.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
                a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
                a.Category = catid > 0 ? db.ArticleCategories.Where(e => e.ID == id).SingleOrDefault() : null;
                return View(a);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }
        public ActionResult List(int id)
        {
            try
            {
                ArticleIndexViewModel a = new ArticleIndexViewModel();
                var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("sitearticle.PageNumber", "page", 1));
                var pageIndex = pageNumber - 1;
                a.Articles = ArticleManager.GetArticles(id, string.Empty, 0, 1, "ID", "desc", pageIndex, Utils.ParseInt(Settings.Default.PageSize));
                a.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
                a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
                a.Category = db.ArticleCategories.Where(e => e.ID == id).SingleOrDefault();
                return View(a);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }
        public ActionResult Details(int id)
        {
            var db = new SDT.Model.SDTDataContext();
            ArticleDetailViewModel a = new ArticleDetailViewModel();
            a.Article = db.Articles.Where(e => e.ID == id).SingleOrDefault();
            a.Article.nViews += 1;
            db.SubmitChanges();
            return View(a);
        }
        public ActionResult Intro(int id)
        {
            var db = new SDT.Model.SDTDataContext();
            ArticleDetailViewModel a = new ArticleDetailViewModel();
            a.Article = db.Articles.Where(e => e.ID == id).SingleOrDefault();
            a.Article.nViews += 1;
            db.SubmitChanges();
            return View(a);
        }
        [ChildActionOnly]
        public ActionResult Focus(int number, int skip, int catid, string title, string order, string orderDir)
        {
            var a = new ArticleListViewModel();
            var l = ArticleManager.GetList(catid, string.Empty, 1, 1, order, orderDir, number, skip);
            a.Category = catid > 0 ? db.ArticleCategories.Where(e => e.ID == catid).SingleOrDefault() : null;
            ViewData["Title"] = title;
            a.Articles = l;
            a.RootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            return View(a);
        }      
        [ChildActionOnly]
        public ActionResult GetTop(int number, int skip, int catid, string title, string order, string orderDir)
        {
            var a = new ArticleListViewModel();
            var l = ArticleManager.GetList(catid, string.Empty, 0, 1, order, orderDir, number, skip);
            ViewData["Title"] = title;
            a.Articles = l;
            a.RootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            return View(a);
        }
        [ChildActionOnly]
        public ActionResult GetByCat(int number, int skip, int catid, string title, string order, string orderDir)
        {
            var a = new ArticleListViewModel();
            var l = ArticleManager.GetList(catid, string.Empty, 0, 1, order, orderDir, number, skip);
            ViewData["Title"] = title;
            a.Articles = l;
            a.RootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            a.Category = db.ArticleCategories.Where(e => e.ID == catid).SingleOrDefault();
            return View(a);
        }
        /// <summary>
        /// Function Name: OtherItems
        /// Description: Get other items in article have catid = catid of current article
        /// </summary>
        /// <param name="id"></param>
        /// <param name="number"></param>
        /// <param name="title"></param>
        /// <param name="greaterYN"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult OtherItems(int id, int number, string title, bool greaterYN)
        {
            var a = new ArticleListViewModel();
            var db = new SDTDataContext();
            var current_article = db.Articles.Where(e => e.ID == id).SingleOrDefault();
            var l = db.Articles.Where(e => e.ArticleCategory.ID == current_article.CatID)
                .Where(e => e.PubYN == true)
                .Where(e => e.ID != id)
                .Where(e => greaterYN == false ? e.AddDate <= current_article.AddDate : e.AddDate >= current_article.AddDate).OrderByDescending(e => e.ID).Take(number).ToList(); ;
            ViewData["Title"] = title;
            a.Articles = l;
            return View(a);
        }
        [ChildActionOnly]
        public ActionResult Guide(int number, int skip, int catid, string title, string aorder, string aorderDir)
        {
            var a = new ArticleListViewModel();
            var l = ArticleManager.GetList(catid, string.Empty, 0, 1, aorder, aorderDir, number, skip);
            ViewData["Title"] = title;
            a.Articles = l;
            a.RootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            a.Category = catid > 0 ? db.ArticleCategories.SingleOrDefault(e => e.ID == catid) : null;
            return View(a);
        }
        [ChildActionOnly]
        public ActionResult Gets(int number, int skip, int catid, string title, string order, string orderDir, string viewName)
        {
            var a = new ArticleListViewModel();
            var l = ArticleManager.GetList(catid, string.Empty, 1, 1, order, orderDir, number, skip);
            ViewData["Title"] = title;
            a.Articles = l;
            a.RootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            return PartialView(viewName, a);
        }
        [ChildActionOnly]
        public ActionResult GetCats(int number, string title, string viewName)
        {
            var l = db.ArticleCategories.Where(e => e.ParentID == null && e.ActiveYN == true)
                .OrderBy(e => e.Priority)
                .Take(number)
                .ToList();
            ViewData["Title"] = title;
            return PartialView(viewName, l);
        }
    }
}
